package cn.longwingstech.intelligence.agent.app.tool;

import cn.hutool.core.io.FileUtil;
import cn.longwingstech.intelligence.agent.common.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

public class FileOperationTool {

    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(description = "读取指定文件的内容")
    public String readFile(@ToolParam(description = "要读取的文件名称") String fileName) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "读取文件出错: " + e.getMessage();
        }
    }

    @Tool(description = "将内容写入到指定文件")
    public String writeFile(
        @ToolParam(description = "要写入的文件名称") String fileName,
        @ToolParam(description = "要写入的文件内容") String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            // 确保目录存在
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content, filePath);
            return "文件成功写入至: " + filePath;
        } catch (Exception e) {
            return "写入文件出错: " + e.getMessage();
        }
    }
}
